package com.example.kehoobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.kehoobackend.common.FollowType;
import com.example.kehoobackend.domain.Follow;
import com.example.kehoobackend.domain.Question;
import com.example.kehoobackend.domain.Topic;
import com.example.kehoobackend.domain.User;
import com.example.kehoobackend.persistance.FollowMapper;
import com.example.kehoobackend.service.FollowService;
import com.example.kehoobackend.service.QuestionService;
import com.example.kehoobackend.service.TopicService;
import com.example.kehoobackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;


@Service("followService")
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {


//    @Autowired
//    QueryWrapper<Follow> queryWrapper;

    @Autowired
    UserService userService;

    @Autowired
    QuestionService questionService;

    @Lazy
    @Autowired
    TopicService topicService;

//    @Autowired
//    QueryWrapper<User> userQueryWrapper;

    @Override
    public boolean followOne(int from_id, int to_id, int type) {
        Follow follow = new Follow();
        follow.setFrom_id(from_id);
        follow.setTo_id(to_id);
        follow.setType(type);
        return save(follow);
    }

    @Override
    public boolean cancelOne(int from_id, int to_id, int type) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_id",from_id).eq("to_id",to_id)
                .eq("type",type);
        Follow follow = getOne(queryWrapper);
        return removeById(follow);
    }

    //可以优化
    @Override
    public Map<String, Object> allFollow(int user_id, int type) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        queryWrapper.eq("from_id",user_id).eq("type",type).select("to_id");
        List<Integer> IDs = get_toIDs(list(queryWrapper));

        if(IDs.isEmpty()){
            map.put("List",null);
            return map;
        }

        switch (type){
            case FollowType.USER :
                List<User> userList = userService.listByIds(IDs);
                map.put("userList",userList);
                break;
            case FollowType.QUESTION :
                List<Question> questionList = questionService.listByIds(IDs);
                map.put("questionList",questionList);
                break;
            case FollowType.TOPIC :
                List<Topic> topicList = topicService.listByIds(IDs);
                map.put("topicList",topicList);
                break;
        }
        return map;
    }

    @Override
    public int countUserFollow(int user_id, int type) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        int counts = 0;
        queryWrapper.eq("from_id",user_id).eq("type",type);
        counts = (int) count(queryWrapper);
        return counts;
    }

    @Override
    public int countFans(int id, int type) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        int counts = 0;
        queryWrapper.eq("to_id",id).eq("type",type);
        counts = (int) count(queryWrapper);
        return counts;
    }

    @Override
    public boolean isFollow(int from_id, int to_id, int type) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("from_id",from_id).eq("to_id",to_id)
                .eq("type",type);
        return getOne(queryWrapper) != null;
    }

    @Override
    public List<User> allFans(int id, int type) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        queryWrapper.eq("to_id",id).eq("type",type);
        List<Follow> followList = list(queryWrapper);
        List<Integer> IDs = get_fromIDs(followList);

        if(IDs.isEmpty()){
            return null;
        }
        else {
            userQueryWrapper.in("user_id",IDs).select("user_id","username");
            List<User> fans = userService.list(userQueryWrapper);
            return fans;
        }
    }

    List<Integer> get_toIDs(List<Follow> followList){
        List<Integer> integers = new ArrayList<>();
        for(Follow follow : followList){
            integers.add(follow.getTo_id());
        }
        return integers;
    }

    List<Integer> get_fromIDs(List<Follow> followList){
        List<Integer> integers = new ArrayList<>();
        for(Follow follow : followList){
            integers.add(follow.getFrom_id());
        }
        return integers;
    }




}
